package com.feriki.msync;

import com.feriki.msync.config.Config;
import com.feriki.msync.process.Engine;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author luozhida
 * @create 2024-07-01
 */
@Slf4j
public class Main {
    private static Config config;

    public static void main(String[] args) {
        try(InputStream inputStream = args.length>0?
             Files.newInputStream(Paths.get(args[0])):
             Main.class.getResourceAsStream("/config.yaml")
        ){
            Yaml yaml = new Yaml();
            config = yaml.loadAs(inputStream, Config.class);
        }catch (Exception e){
            log.error("读取{}出现异常!",args.length>0?args[0]:"config.yaml",e);
            System.exit(-1);
        }

        try{
            long startTimeMs = System.currentTimeMillis();
            Engine channel = new Engine(config);
            channel.start();
            log.info("任务完成,总耗时:{}毫秒", System.currentTimeMillis() - startTimeMs);
        }catch (Exception e){
            log.error("任务执行出现异常!",e);
            System.exit(-1);
        }

    }
}